package Action;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import Utils.*;
import Bean.CategoryBean;
import Bean.ProductBean;

public class Category {
	public boolean insertCategory(CategoryBean bean, Connection conn,
			String _lang) {
		String sql = "insert into category (id, name" + _lang + ", position) "
				+ " values (?,?,?)";
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, bean.getId());
			stmt.setString(2, bean.getName());
			stmt.setInt(3, bean.getPosition());
			stmt.executeUpdate();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			if (stmt != null) {
				try {
					stmt.close();
				} catch (Exception e) {
				}
			}
		}

	}

	public String getCategoryList(Connection conn) {
		String sql = "select * from category order by position";
		String xml = "";

		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			stmt = conn.prepareStatement(sql);
			rs = stmt.executeQuery();
			
			while (rs.next()) {
				xml = "<category name = \"" + rs.getString("name") + "\" position=\"" + rs.getInt("position") + "\"/>";
			}
			return Common.makeSuccessXML(xml);
		} catch (Exception e) {
			e.printStackTrace();
			return Common.makeErrorXML("Error");
		} finally {
			if (rs != null)
				try {
					rs.close();
				} catch (Exception e) {
				}
			if (stmt != null)
				try {
					stmt.close();
				} catch (Exception e) {
				}
		}
	}
}
